<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib tagdir="/WEB-INF/tags" prefix="lookup" %>
<%@ page import="com.eischet.lookup.web.*" %>
<%@ page import="org.apache.lucene.document.Field" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.Map" %>
<%@ page import="static com.eischet.lookup.Functional.firstOf" %>
<%@ page import="static com.eischet.lookup.StringUtilities.escapeForHtml" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<jsp:useBean id="elapsedTime" scope="request" type="java.lang.String"/>
<%
    Pages requestedPage = (Pages) request.getAttribute("page");
    if (requestedPage == null) {
        response.sendRedirect("lookup.app");
    }

//    String elapsedTime = (String) request.getAttribute("elapsedTime");
    String userQuery = (String) request.getAttribute("userQuery");
    String type = (String) request.getAttribute("type");
    int startIndex = (Integer) request.getAttribute("startIndex");
    int pageSize = (Integer) request.getAttribute("pageSize");
    String fromDate = (String) request.getAttribute("fromDate");
    String toDate = (String) request.getAttribute("toDate");
    List<SearchResult> results = (List<SearchResult>) request.getAttribute("results");
    int numberOfHits = (Integer) request.getAttribute("numberOfHits");
    String error = (String) request.getAttribute("error");
    SearchIndex searchIndex = (SearchIndex) request.getAttribute("searchIndex");
    String loginName = (String) request.getAttribute("loginName");
    String loginDomain = (String) request.getAttribute("loginDomain");
    LuceneQuery lucene = (LuceneQuery) request.getAttribute("lucene");
    Map<String, Long> statistics = (Map<String, Long>) request.getAttribute("statistics");
    String requestedHandle = (String) request.getAttribute("requestedHandle");
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
    <head>
        <title>lookup</title>
        <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
        <link rel="stylesheet" type="text/css" href="css/ui-lightness/jquery-ui-1.8.11.custom.css">
        <link rel="stylesheet" type="text/css" href="css/lookup.css">
        <script type="text/javascript" src="js/jquery-1.5.1.min.js"></script>
        <script type="text/javascript" src="js/jquery-ui-1.8.11.custom.min.js"></script>
        <script type="text/javascript" src="js/jquery.clockpick.js"></script>
        <script type="text/javascript" src="js/jquery-ui-timepicker-addon.js"></script>
        <script type="text/javascript" src="js/lookup.js"></script>
    </head>
    <body>
        <div id="menubar">
            <span class="apptitle">Lookup 2.0</span>
            <span class="menu">
                <span class="menuitem">
                    <a href="lookup.app">Search</a>
                </span>
                <span class="menuitem">
                    <a href="lookup.app?do=h">Help</a>
                </span>
                <span class="menuitem">
                    <a href="lookup.app?do=a">About</a>
                </span>
            </span>
            <span class="login">
                Logged in as <%= loginName %>
            </span>
        </div>

        <% if (Pages.SearchForm.equals(requestedPage)) { %>

            <div class="largetitle"><img src="gfx/logos/lookupsmall.gif" alt="Lookup Logo"></div>
            <div class="qforml">
                <form method="GET" name="f" action="lookup.app">
                    <div>
                        <input type="hidden" name="d" value="r">
                        <div class="qforml">
                            <em>Search for:</em>
                            <input type="text" size="60" name="q" value="<%=userQuery%>">
                            <em>Filter by:</em>
                            <lookup:filter name="t" searchIndex="${searchIndex}" selectedType="${type}"/>
                            <input type="hidden" name="do" value="r">
                            <input type="submit" value="Find">
                        </div>
                        <div class="daterange">
                            <em>Updated between</em>
                            <span class="date">
                                <input type="text" class="date" id="fromdate1" name="fromdate" value="<%=fromDate%>">
                            </span>
                            <em>and</em>
                            <span class="date">
                                <input type="text" class="date" id="todate1" name="todate" value="<%=toDate%>">
                            </span>
                        </div>
                    </div>
                </form>
            </div>

        <% } else if (Pages.Results.equals(requestedPage)) {
            int length = results.size();
        %>
            <div class="searchformsmall">
                <table>
                    <tr>
                        <td class="smalllogo">
                            <a class="img" href="lookup.app">
                                <img src="gfx/logos/lookupxsmall.gif" alt='logo'>
                            </a>
                        </td>
                        <td class="smallbox">
                            <form method="GET" name="fs" action="lookup.app">
                                <div>
                                    <span class="wide">
                                            <input type="text" size="60" name="q" value="<%=userQuery%>">
                                    </span>
                                    <span class="wide">
                                        <lookup:filter name="t" searchIndex="${searchIndex}" selectedType="${type}"/>
                                    </span>
                                    <span class="wide">
                                        <input type="submit" value="Find">
                                    </span>
                                    <input type="hidden" name="do" value="r">
                                    <div class="daterange">
                                        <em>Updated between</em>
                                        <span class="date">
                                            <input type="text" class="date" id="fromdate" name="fromdate" value="<%=fromDate%>">
                                        </span>
                                        <em>and</em>
                                        <span class="date">
                                            <input type="text" class="date" id="todate" name="todate" value="<%=toDate%>">
                                        </span>
                                    </div>
                                </div>
                            </form>
                        </td>
                    </tr>
                </table>
            </div>
            <div class="main">
                <% if (numberOfHits == 0) { %>
                    <h1>No hits</h1>
                    <div class='directions'>
                        The query you entered returned no results.
                        <% if (error != null && !error.isEmpty()) { %>
                            <div class='error'>
                                <%=error%>
                            </div>
                        <% } %>
                    </div>
                <% } else { %>
                    <h1><%=numberOfHits%> hits <span> showing <%=startIndex+1%> - <%= startIndex + length%></span></h1>
                    <% if (numberOfHits > pageSize) { %>
                        <lookup:pager userQuery="<%=userQuery%>" type="<%=type%>" total="<%=numberOfHits%>" length="<%=length%>"
                                      startIndex="<%=startIndex%>" pageSize="<%=pageSize%>" fromDate="<%=fromDate%>" toDate="<%=toDate%>"/>
                    <% } %>
                    <c:forEach var="searchResult" items="${results}">
                        <lookup:displaySearchResult doc="${searchResult}" />
                    </c:forEach>


                    <lookup:pager userQuery="<%=userQuery%>" type="<%=type%>" total="<%=numberOfHits%>" length="<%=length%>"
                                  startIndex="<%=startIndex%>" pageSize="<%=pageSize%>" fromDate="<%=fromDate%>" toDate="<%=toDate%>"/>
                <% } %>
            </div>
        <% } else if (Pages.About.equals(requestedPage)) { %>
            <div class="directions">

                <h2>Lookup Full Text Search Application</h2>
                <div class="pp">
                    Lookup@ITSM &copy; 2011 Swisscom Schweiz AG.
                </div>
                <div class="pp">
                    ARS Consulting and Project Management: FROX Communication AG (<a href="http://www.frox.com/">www.frox.com</a>)
                </div>
                <div class="pp">
                    Based on Lookup@assyst by Axios Systems GmbH (<a href="http://www.axiossystems.com/">www.axiossystems.com</a>)
                </div>
                <div class="pp">
                    <img src="gfx/databases.png" alt="SES logo">
                    <br>
                    Idea &amp; Development: Stefan Eischet Software (<a href="http://www.eischet.com/">www.eischet.com</a>)
                </div>

                <h2>Index Statistics</h2>
                <div class="pp">
                    <table class="statistics">
                        <tr>
                            <th>Searchable Object Type</th>
                            <th>Type Code</th>
                            <th>Number Of Objects</th>
                        </tr>
                        <% for (SearchableCategory category: searchIndex.getSearchableCategories()) { %>
                            <% if (category.isSelectable()) { %>
                                <tr>
                                    <td><%=category.getTitle()%></td>
                                    <td><%=category.getName()%></td>
                                    <td><%=statistics.get(category.getName())%></td>
                                </tr>
                            <% } %>
                            <% for (SearchableType searchableType: category.getSearchableTypes()) { %>
                                <tr>
                                    <td><%=searchableType.getTitle()%></td>
                                    <td><%=searchableType.getName()%></td>
                                    <td><%=statistics.get(searchableType.getName())%></td>
                                </tr>
                            <% } %>
                        <% } %>
                    </table>
                </div>
            </div>
        <% } else if (Pages.Help.equals(requestedPage)) { %>
            <lookup:help/>
        <% } else if (Pages.RawView.equals(requestedPage)) {
            %><div class="main">
                <h1>Index Structure view for <%= requestedHandle %></h1><%
            SearchResult searchResult = firstOf(results);
            if (searchResult == null) {
                %><div class='error'>The requested document was not found or is not accessible with your user rights!</div><%
            } else { %>
                <div class="dump">
                    <table class="data">
                        <tr>
                            <th>Key</th>
                            <th>#</th>
                            <th>Values</th>
                            <th>Tokenized</th>
                            <th>Indexed</th>
                        </tr>
                        <% int countFields = 0; %>
                        <% for (String fieldName: searchResult.getKeys()) {
                            List<Field> fields = searchResult.getFields(fieldName);
                            Field first = fields.get(0);
                        %>
                            <%
                                if ("all".equals(fieldName)) continue; // skip the "all" conglomerate field
                                ++countFields;
                            %>
                            <tr>
                                <td><%= escapeForHtml(fieldName) %></td>
                                <td><%= fields.size() %></td>
                                <td>
                                    <% for (Field f: fields) { %>
                                        <%= escapeForHtml(f.stringValue()) %>
                                    <br>
                                    <% } %>
                                </td>
                                <td>
                                    <%= first.isTokenized() %>
                                </td>
                                <td>
                                    <%= first.isIndexed() %>
                                </td>
                            </tr>
                        <% } %>
                        <% if (countFields == 0) { %>
                            <tr>
                                <td colspan="5" class="empty">No data found!</td>
                            </tr>
                        <% } %>
                    </table>
                </div>
                <% } %>
            </div>
        <% } else { %>
            Page not implemented: <%=requestedPage%>
        <% } %>

        <div class="footer">
        <c:if test="${not empty elapsedTime}">
            <div class='timer' title="elapsed time on server / last known index update">
                ${elapsedTime} seconds
                <c:if test="${not empty mostRecentWatermark}">
                    &mdash;
                    latest index update ${mostRecentWatermark}
                </c:if>
            </div>
        </c:if>
        </div>

    </body>
</html>
